<template>
  <div class="app-container">
    <span style="font-size: medium;color: rgba(151,170,195) "> / 采购调拨 / 调拨计划</span>

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" style="margin-top: 20px">
      <el-form-item label="调拨编号" prop="allocationNo">
        <el-input
            v-model="queryParams.allocationNo"
            placeholder="请输入调拨编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编号" prop="warehouseNo">
        <el-input
            v-model="queryParams.warehouseNo"
            placeholder="请输入仓库编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择计划状态" clearable>
          <el-option
              v-for="dict in allocation_plan_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增调拨计划</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AllocationPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="调拨编号" align="center" :show-overflow-tooltip="true">
        <template #default="scope">
          <router-link :to="'/AllocationDetail/index/' + scope.row.allocationNo" class="link-type">
            <span>{{ scope.row.allocationNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderNo" width="130"/>
      <el-table-column label="调拨时间" align="center" prop="allocationDate">
        <template #default="scope">
          <span>{{ parseTime(scope.row.allocationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库编号" align="center" prop="warehouseNo" width="130"/>
      <el-table-column label="计划状态" align="center" prop="state">
        <template #default="scope">
          <el-tag :type="scope.row.state === '待生成运输订单' ? 'danger' : 'warning'" disable-transitions>
            {{ scope.row.state}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" header-align="center" prop="remarks"/>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="160px">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup>
import {parseTime} from "../../utils/ruoyi.js";
import { listAllocationPlan, delAllocationPlan, } from "@/api/purchase/AllocationPlan.js";

const { proxy } = getCurrentInstance();
const { allocation_plan_status } = proxy.useDict('allocation_plan_status');

const AllocationPlanList = ref([]);
const allocationDetails = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const orderNo = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    allocationNo: null,
    orderNo: null,
    deliveryName: null,
    warehouseNo: null,
    state: null,
  },
  rules: {
    orderNo: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
    allocationDate: [
      { required: true, message: "调拨时间不能为空", trigger: "blur" }
    ],
    deliveryCode: [
      { required: true, message: "物流商编码不能为空", trigger: "blur" }
    ],
    warehouseNo: [
      { required: true, message: "仓库编号不能为空", trigger: "blur" }
    ],
    shippingPlace: [
      { required: true, message: "发货地不能为空", trigger: "blur" }
    ],
    planStatus: [
      { required: true, message: "计划状态不能为空", trigger: "change" }
    ],
    modifiedDate: [
      { required: true, message: "修改日期不能为空", trigger: "blur" }
    ],
    modifiedBy: [
      { required: true, message: "修改人不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询AllocationPlan列表 */
function getList() {
  loading.value = true;
  queryParams.value.orderNo = orderNo.value;
  listAllocationPlan(queryParams.value).then(response => {
    AllocationPlanList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 表单重置
function reset() {
  form.value = {
    allocationNo: null,
    orderNo: null,
    allocationDate: null,
    deliveryCode: null,
    deliveryName: null,
    warehouseNo: null,
    shippingPlace: null,
    planStatus: null,
    createdTime: null,
    modifiedDate: null,
    modifiedBy: null,
    remarks: null
  };
  proxy.resetForm("AllocationPlanRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.orderNo = orderNo.value;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.allocationNo);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$router.push({
    path:'/NewAllocation',
    query:{
      orderNo: orderNo.value
    }
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  if(row.state === 1){
    proxy.$modal.msgError("该计划已分配运输订单，不允许修改");
    return;
  }
  proxy.$router.push({
    path:'/NewAllocation',
    query:{
      orderNo: orderNo.value,
      allocationNo: row.allocationNo
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _allocationNos = row.allocationNo || ids.value;
  proxy.$modal.confirm('是否确认删除调拨编号为"' + _allocationNos + '"的计划？').then(function() {
    return delAllocationPlan(_allocationNos);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('AllocationPlan/export', {
    ...queryParams.value
  }, `AllocationPlan_${new Date().getTime()}.xlsx`)
}

function initial(){
  if(proxy.$route.query.orderNo){
    orderNo.value = proxy.$route.query.orderNo;
    getList();
  }
}

initial()
</script>

<style scoped lang="scss">
:deep(.el-input__wrapper) {
  position: relative;
  .el-input__inner {
    padding-right: 18px;
  }
  .el-input__suffix {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
  }
}
</style>